package com.powerband.apiheartrate.heartrate

import android.telephony.CellIdentityNr
import com.powerband.apiheartrate.base.FBKSummaryType
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.concurrent.timer


class DataAccessMgr(val delegate: LogBufferDelegate) {
//    var start: Long = 0
//    var end: Long = 0
    var page = 0
    var total = 0
    var steptotal = 0
    var isSport = false
    var type: FBKSummaryType = FBKSummaryType.HeartRate // 0 richang 1 shuimian / 2 yundong / lishi
    private val dateFmt: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    private var records: MutableList<DaySumRecord> = ArrayList()
    private var detailBuffer: MutableList<DaySumDetail> = ArrayList()
    private var currentStamp: Long = 0

    private var dataBuffer: MutableList<Byte> = ArrayList()


    private var sleepRecords: MutableMap<Long, List<Byte>> = HashMap()
    private var currentSleepType: Byte = 0x05

    private var sportsRecords: MutableMap<Long, List<Byte>> = HashMap()
    private var currentSportType: Byte = 0x05

    private var historyBuffer: MutableList<HistoryDetail> = ArrayList()
    private var dailyHistoryRecords: MutableMap<Long, List<HistoryDetail>> = HashMap()
    private var sportsHistoryRecords: MutableMap<Long, List<HistoryDetail>> = HashMap()

    private var sleepOrExcerArray: MutableList<Map<Long, List<Byte>>> = ArrayList()
    private var sleepSumArray: MutableList<SleepItem> = ArrayList()
    private var dailyOrHistoryArray: MutableList<Map<Long, List<HistoryDetail>>> = ArrayList()

    private var motionDataBuffer: MutableList<HistoryDetail> = ArrayList()

//    fun hasData(start1: Long, end1: Long, type: FBKSummaryType): Boolean {
//        var ans = when (type) {
//            FBKSummaryType.Sleep -> sleepRecords.isNotEmpty()
//            FBKSummaryType.Sport -> sportsRecords.isNotEmpty()
//            else -> records.size > 0
//        }
//        return start == start1 && end == end1 && ans
//    }

    fun updateData(data: ByteArray) {
        dataBuffer.addAll(data.drop(1))
        page += data.size - 1
        if (total == 0 || page % total == 0) {
            delegate.logOutBufferData(dataBuffer.toList())
            when (type) {
                FBKSummaryType.Sleep -> parseSleepData()
                FBKSummaryType.Sport -> parseSportData()
                FBKSummaryType.ModeDaily -> parseMotionDaily()
                FBKSummaryType.ModeSport -> parseMotionSport()
                else -> parseDataAll()
            }
        }
    }

    private fun parseDataAll() {
        var offset = 0
        val total = dataBuffer.size
        while (offset <= total) {
            if (offset == 0 && total > 0) {
                val data = dataBuffer.subList(offset, offset + 4)
                val time = (data[0].toInt() and 0xFF) or
                        (data[1].toInt() and 0xFF).shl(8) or
                        (data[2].toInt() and 0xFF).shl(16) or
                        (data[3].toInt() and 0xFF).shl(24)
                currentStamp = time.toLong() * 1000
                offset += 4
            } else if (detailBuffer.size == 24) {
                records.add(DaySumRecord(currentStamp, detailBuffer.toList()))
                detailBuffer.clear()
                if (offset < total - 4) {
                    val data = dataBuffer.subList(offset, offset + 4)
                    val time = (data[0].toInt() and 0xFF) or
                            (data[1].toInt() and 0xFF).shl(8) or
                            (data[2].toInt() and 0xFF).shl(16) or
                            (data[3].toInt() and 0xFF).shl(24)
                    currentStamp = time.toLong() * 1000
                    offset += 4
                }
            } else {
                if (offset <= total - 20) {
                    val data = dataBuffer.subList(offset, offset + 20)

                    detailBuffer.add(
                        DaySumDetail(
                            ((data[1].toInt() and 0xFF) or (data[0].toInt() and 0xFF).shl(8)).filterValue(),
                            0,
                            0,
                            0,
                            0,
                            (data[7].toInt() and 0xFF).filterValue(),
                            0,
                            (data[9].toInt() and 0xFF).filterValue(),
                            0,
                            0,
                            (data[14].toInt() and 0xFF).filterValue(),
                            (data[15].toInt() and 0xFF).filterValue(),
                            (data[16].toInt() and 0xFF).filterValue(),
                            (data[17].toInt() and 0xFF).filterValue(),
                            ((data[11].toInt() and 0xFF) or (data[10].toInt() and 0xFF).shl(8)).filterValue(),
                            ((data[19].toInt() and 0xFF) or (data[18].toInt() and 0xFF).shl(8)).filterValue(),
                        )
                    )
                }
                offset += 20
            }
        }
    }

    private fun Int.filterValue(): Int {
        if (this == 0xff || this == 0xffff) {
            return 0
        }
        return this
    }

    public fun parseSleepData() {
//        val hexSSS =
//            "404390642101C2005F001604130104190204 23 0104 30 02 04 39 01 05 07 0205 12 01 05 24 02 05 2D 01 06 08 02 06 12 01 06 20 02 06 26 01 06 36 02 07 05 01 07 18 02 07 23 01 07 2B 02 07 35 01 08 26 02 08 2E 01 09 08 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"
//        val hexString = hexSSS.replace(" ", "")
//        val byteArray = ByteArray(hexString.length / 2)
//
//        for (i in byteArray.indices) {
//            val index = i * 2
//            val byteStr = hexString.substring(index, index + 2)
//            byteArray[i] = byteStr.toInt(16).toByte()
//        }
//        dataBuffer.addAll(byteArray.toList())

        val days = dataBuffer.size / 191
        for (i in 0 until days) {
            if ((i + 1) * 191 > dataBuffer.size) {
                continue
            }
            val data = dataBuffer.subList(i * 191, (i + 1) * 191)
            val time = (data[0].toInt() and 0xFF) or
                    (data[1].toInt() and 0xFF).shl(8) or
                    (data[2].toInt() and 0xFF).shl(16) or
                    (data[3].toInt() and 0xFF).shl(24)
            currentStamp = time * 1000L
            val slpAll = (data[5].toInt() and 0xFF).shl(8) or (data[4].toInt() and 0xFF)
            val lowAll = (data[7].toInt() and 0xFF).shl(8) or (data[6].toInt() and 0xFF)
            val deepAll = (data[9].toInt() and 0xFF).shl(8) or (data[8].toInt() and 0xFF)

            val totalSeg = data[10].toInt() and 0xFF

            val tempSeg: MutableList<SleepSeg> = ArrayList()
            var curIndex = 0

            if (totalSeg > 60) {
                continue
            }
            val subD = data.subList(
                11,
                11 + 3 * totalSeg
            )
            val calendar = Calendar.getInstance(Locale.CHINA)
            calendar.time = Date(currentStamp)
            val y1 = calendar.get(Calendar.YEAR)
            val m1 = calendar.get(Calendar.MONTH) + 1
            val d1 = calendar.get(Calendar.DAY_OF_MONTH)
            calendar.time = addingTimeInterval(calendar.time, 24 * 60 * 60)
            val y2 = calendar.get(Calendar.YEAR)
            val m2 = calendar.get(Calendar.MONTH) + 1
            val d2 = calendar.get(Calendar.DAY_OF_MONTH)

            while (curIndex <= subD.size - 3) {
                val triSub = subD.subList(curIndex, curIndex + 3)
                curIndex += 3
                tempSeg.add(
                    SleepSeg(
                        triSub[2],
                        fetchDateStr(y1, y2, m1, m2, d1, d2, triSub[0].toInt() and 0xFF, triSub[1].toInt() and 0xFF)
                    )
                )

            }
            sleepSumArray.add(
                SleepItem(
                    dateFmt.format(Date(currentStamp)),
                    totalSeg,
                    slpAll,
                    lowAll,
                    deepAll,
                    tempSeg
                )
            )
        }
        //getSummarySleep()
    }

    private fun fetchDateStr(
        y: Int,
        y1: Int,
        m: Int,
        m1: Int,
        d: Int,
        d1: Int,
        h: Int,
        mi: Int
    ): String {
        return if (h >= 20) String.format(
            "%04d-%02d-%02d %02d:%02d:00",
            y,
            m,
            d,
            h,
            mi
        ) else String.format("%04d-%02d-%02d %02d:%02d:00", y1, m1, d1, h, mi)
    }

    private fun parseSportData() {
        var offset = 0
        val total = dataBuffer.size
        while (offset < total - 4) {
            val data = dataBuffer.subList(offset, offset + 4)
            val time = (data[0].toInt() and 0xFF) or
                    (data[1].toInt() and 0xFF).shl(8) or
                    (data[2].toInt() and 0xFF).shl(16) or
                    (data[3].toInt() and 0xFF).shl(24)
            currentStamp = time.toLong() * 1000
            offset += 4

            if (offset + 288 <= total) {
                val mp = mapOf<Long, List<Byte>>(
                    currentStamp to dataBuffer.subList(
                        offset,
                        offset + 288
                    )
                )
                sleepOrExcerArray.add(mp)
                offset += 288
            }
        }
    }

    private fun parseMotionDaily() {
        var offset = 0
        val total = dataBuffer.size
        while (offset <= total - 13) {
            val sub = dataBuffer.subList(offset, offset + 13)
            offset += 13
            val time = (sub[0].toInt() and 0xFF) or
                    (sub[1].toInt() and 0xFF).shl(8) or
                    (sub[2].toInt() and 0xFF).shl(16) or
                    (sub[3].toInt() and 0xFF).shl(24)
            if ((sub[3].toInt() and 0xFF) == 0xFF) {
                continue
            }
            val start = time - 299

            val step = (sub[6].toInt() and 0xFF).shl(8) or (sub[7].toInt() and 0xFF)
            val slp = (sub[11].toInt() and 0xFF).shl(8) or (sub[12].toInt() and 0xFF)
            val rate = sub[8].toInt() and 0xFF
            val pre = sub[9].toInt() and 0xFF
            val ox = sub[10].toInt() and 0xFF

            motionDataBuffer.add(
                HistoryDetail(
                    dateFmt.format(Date(start * 1000L)),
                    dateFmt.format(Date(time * 1000L)),
                    sub[4].toInt(),
                    sub[5].toInt(),
                    step.filterValue(),
                    rate.filterValue(),
                    pre.filterValue(),
                    ox.filterValue(),
                    slp.filterValue()
                )
            )
        }
    }

    private fun parseMotionSport() {
        var offset = 0
        val total = dataBuffer.size
        while (offset < total - 4) {
            val data = dataBuffer.subList(offset, offset + 4)
            val time = (data[0].toInt() and 0xFF) or
                    (data[1].toInt() and 0xFF).shl(8) or
                    (data[2].toInt() and 0xFF).shl(16) or
                    (data[3].toInt() and 0xFF).shl(24)
            currentStamp = time.toLong() * 1000
            offset += 4
            var len = 0
            while (len < 60 && offset + 2 <= total) {
                val sub = dataBuffer.subList(offset, offset + 2)
                offset += 2
                len += 1
                val ty = sub[0].toInt() % 16
                val ht = sub[1].toInt() and 0xFF

                if (ht == 0xFF) {
                    continue
                }
                historyBuffer.add(
                    HistoryDetail(
                        "",
                        "",
                        ty,
                        0,
                        0,
                        ht,
                        0,
                        0,
                        0
                    )
                )
            }
            dailyOrHistoryArray.add(mapOf(currentStamp to historyBuffer.toList()))
            historyBuffer.clear()
        }
    }

    fun clearData() {
        page = 0
        sleepRecords.clear()
        sportsRecords.clear()
        records.clear()
        dataBuffer.clear()
        historyBuffer.clear()
        motionDataBuffer.clear()
        dailyHistoryRecords.clear()
        sportsHistoryRecords.clear()
        sleepOrExcerArray.clear()
        sleepSumArray.clear()
        dailyOrHistoryArray.clear()

    }

    fun mapDataT(type: Int): String {
        return when (type) {
            1 -> "heartRate"
            2, 3 -> "steps"
            4, 5 -> "sleep"
            else -> "unkown"
        }
    }

    fun getDataByType(type: FBKSummaryType): List<Map<String, Any>> {
        return when (type) {
            FBKSummaryType.HeartRate -> getSummaryHeartRate()
            FBKSummaryType.Step -> getStepSummary()
            FBKSummaryType.Oxgen -> getSummaryBloodOxygen()
            FBKSummaryType.Pressure -> getSummaryPressureIndex()
            FBKSummaryType.Calorie -> getSummaryCalorie()
            else -> emptyList()
        }
    }

    private fun getSummaryHeartRate(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in records) {
            val start = Date(item.timeStart)
            val end = addingTimeInterval(start, 24 * 60 * 60 - 1)
            val tempValue = mutableListOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
            item.detail.map {
                tempValue[0] = tempValue[0].toInt().coerceAtLeast(it.maxRate).toDouble()
                if (it.minRate > 0) {
                    if (tempValue[1].toInt() == 0) {
                        tempValue[1] = it.minRate.toDouble()
                    }
                    tempValue[1] = tempValue[1].toInt().coerceAtMost(it.minRate).toDouble()
                }
                tempValue[2] += it.avRateAccum
                if (it.dlRata > 0) {
                    tempValue[3] = (tempValue[3] + it.dlRata) / 2.0
                }
                if (it.spRate > 0) {
                    tempValue[4] = (tempValue[4] + it.spRate) / 2.0
                }

                tempValue[5] = tempValue[5] + it.dlRateDuration
                tempValue[6] = tempValue[6] + it.spRateDuration
            }
            val sumTime = tempValue[5] + tempValue[6]
            val avRa = tempValue[2] / (if (sumTime.toInt() == 0) 1.0 else sumTime)
            ret.add(
                mapOf<String, Any>(
                    "startDate" to dateFmt.format(start),
                    "endDate" to dateFmt.format(end),
                    "highestHeartRate" to tempValue[0].toInt(),
                    "lowestHeartRate" to tempValue[1].toInt(),
                    "averageHeartRate" to avRa.toFloat(),
                    "dailyAverageHeartRate" to tempValue[3].toFloat(),
                    "sportAverageHeartRate" to tempValue[4].toFloat(),
                    "heartRateTotalTime" to sumTime,
                    "dailyHeartRateTotalTime" to tempValue[5],
                    "sportHeartRateTotalTime" to tempValue[6]
                )
            )
        }
        return ret
    }

    private fun getStepSummary(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in records) {
            val start = Date(item.timeStart)
            val end = addingTimeInterval(start, 24 * 60 * 60 - 1)
            val stepHour: MutableList<Map<String, Any>> = ArrayList()
            var totalStep = 0
            item.detail.forEachIndexed { index, it ->
                var hourDate = addingTimeInterval(start, 60 * 60 * index.toLong())
                var hourEnd = addingTimeInterval(hourDate, 60 * 59)


                totalStep += it.lowStep
                stepHour.add(
                    mapOf<String, Any>(
                        "startDate" to dateFmt.format(hourDate),
                        "endDate" to dateFmt.format(hourEnd),
                        "steps" to it.lowStep
                    )
                )
            }
            ret.add(
                mapOf<String, Any>(
                    "startDate" to dateFmt.format(start),
                    "endDate" to dateFmt.format(end),
                    "steps" to totalStep,
                    "stepsByHour" to stepHour
                )
            )
        }
        return ret
    }

    private fun getSummaryBloodOxygen(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in records) {
            val start = Date(item.timeStart)
            val end = addingTimeInterval(start, 24 * 60 * 60 - 1)
            var hb = 0
            var lb = 0
            var av: Double = 0.0
            item.detail.forEach {
                hb = hb.coerceAtLeast(it.oxgen)
                if (it.oxgen > 0) {
                    lb = if (lb == 0) {
                        it.oxgen
                    } else {
                        lb.coerceAtMost(it.oxgen)
                    }
                    av = (av + it.oxgen) / 2.0
                }
            }
            ret.add(
                mapOf<String, Any>(
                    "startDate" to dateFmt.format(start),
                    "endDate" to dateFmt.format(end),
                    "highestBloodOxygen" to hb,
                    "lowestBloodOxygen" to lb,
                    "averageBloodOxygen" to av.toFloat()
                )
            )
        }
        return ret
    }

    private fun getSummaryPressureIndex(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in records) {
            val start = Date(item.timeStart)
            val end = addingTimeInterval(start, 24 * 60 * 60 - 1)
            var hb = 0
            var lb = 0
            var av: Double = 0.0
            item.detail.forEach {
                hb = hb.coerceAtLeast(it.pressure)
                if (it.pressure > 0) {
                    if (lb == 0) {
                        lb = it.pressure
                    } else {
                        lb = lb.coerceAtMost(it.pressure)
                    }
                    av = (av + it.pressure) / 2
                }
            }
            ret.add(
                mapOf<String, Any>(
                    "startDate" to dateFmt.format(start),
                    "endDate" to dateFmt.format(end),
                    "highestPressureIndex" to hb,
                    "lowestPressureIndex" to lb,
                    "averagePressureIndex" to av.toFloat()
                )
            )
        }
        return ret
    }

    private fun getSummaryCalorie(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in records) {
            val start = Date(item.timeStart)
            val end = addingTimeInterval(start, 24 * 60 * 60 - 1)
            val calorieByHour: MutableList<Map<String, Any>> = ArrayList()
            var totalCalorie = 0.0
            item.detail.forEachIndexed { index, it ->
                val sp = it.lowStep
                val cal = sp * 0.55 * 60 * 1.036 / 1000
                totalCalorie += cal
                val hourDate = addingTimeInterval(start, 60 * 60 * index.toLong())
                val hourEnd = addingTimeInterval(hourDate, 60 * 59)
                calorieByHour.add(
                    mapOf<String, Any>(
                        "startDate" to dateFmt.format(hourDate),
                        "endDate" to dateFmt.format(hourEnd),
                        "calorie" to cal
                    )
                )
            }
            ret.add(
                mapOf<String, Any>(
                    "startDate" to dateFmt.format(start),
                    "endDate" to dateFmt.format(end),
                    "totalCalorie" to totalCalorie,
                    "calorieByHour" to calorieByHour
                )
            )
        }
        return ret
    }

    fun getSummarySleep(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in sleepSumArray) {
            val awakeTime: MutableList<Map<String, String>> = ArrayList()
            val lightSleepTime: MutableList<Map<String, String>> = ArrayList()
            val deepSleepTime: MutableList<Map<String, String>> = ArrayList()

            var front = 0
            var end = 0
            var lastOne = ""

            while (end < item.datas.size) {

                val itemA = item.datas[front]
                val itemB = item.datas[end]
                // last record
                if (end + 1 == item.datas.size) {
                    val timeSpan =  mapOf(
                        "startDate" to itemA.dateStr,
                        "endDate" to itemB.dateStr
                    )
                    when (itemA.type.toInt()) {
                        0x00 -> {
                            awakeTime.add(timeSpan)
                        }
                        0x01 -> {
                            lightSleepTime.add(timeSpan)
                        }
                        0x02 -> {
                            deepSleepTime.add(timeSpan)
                        }
                        else -> {}
                    }
                    lastOne = itemB.dateStr
                    break
                }

                if (itemA.type == itemB.type) {
                    end += 1
                    continue
                }

                val timeSpan =  mapOf(
                    "startDate" to itemA.dateStr,
                    "endDate" to itemB.dateStr
                )

                when (itemA.type.toInt()) {
                    0x00 -> {
                        awakeTime.add(timeSpan)
                    }
                    0x01 -> {
                        lightSleepTime.add(timeSpan)
                    }
                    0x02 -> {
                        deepSleepTime.add(timeSpan)
                    }
                    else -> {}
                }

                front = end
                end += 1
            }
            var earlyone = lightSleepTime.firstOrNull()?.get("startDate") ?: ""
            if (earlyone.isEmpty()) {
                earlyone = deepSleepTime.firstOrNull()?.get("startDate") ?: ""
            }
            ret.add(
                mapOf(
                    "startDate" to earlyone,
                    "endDate" to lastOne,
                    "awakeTime" to awakeTime,
                    "lightSleepTime" to lightSleepTime,
                    "deepSleepTime" to deepSleepTime,
                    "sleepSum" to item.total,
                    "lowSum" to item.low,
                    "deepSum" to item.depp
                )
            )

        }
        return ret
    }


    fun getExerciseData(): List<Map<String, Any>> {
        currentSportType = 0x05
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (dict in sleepOrExcerArray) {
            for (item in dict) {
                val start = Date(item.key)
                val noExerciseTime: MutableList<Map<String, String>> = ArrayList()
                val heavyExerciseTime: MutableList<Map<String, String>> = ArrayList()
                val lightExerciseTime: MutableList<Map<String, String>> = ArrayList()
                var left = 0
                var right = 0
                var len = item.value.size
                for (span in item.value) {
                    if (right == len - 1) {
                        right++
                    }
                    if (span == currentSportType && len - 1 > right) {
                        right++
                    } else {
                        if (currentSportType.toInt() == 0x05) {
                            currentSportType = span
                            right++
                        } else {
                            val s1 = addingTimeInterval(start, (left * 60 * 5).toLong())
                            val e1 = addingTimeInterval(start, (right * 60 * 5).toLong())
                            when (currentSportType.toInt()) {
                                0x00 -> {
                                    noExerciseTime.add(
                                        mapOf(
                                            "startDate" to dateFmt.format(s1),
                                            "endDate" to dateFmt.format(e1)
                                        )
                                    )
                                }
                                0x01 -> {
                                    lightExerciseTime.add(
                                        mapOf(
                                            "startDate" to dateFmt.format(s1),
                                            "endDate" to dateFmt.format(e1)
                                        )
                                    )
                                }
                                0x02 -> {
                                    heavyExerciseTime.add(
                                        mapOf(
                                            "startDate" to dateFmt.format(s1),
                                            "endDate" to dateFmt.format(e1)
                                        )
                                    )
                                }
                                else -> {}
                            }
                            left = right
                            right++
                            currentSportType = span
                        }
                    }

                }
                ret.add(
                    mapOf(
                        "noExerciseTime" to noExerciseTime,
                        "lightExerciseTime" to lightExerciseTime,
                        "heavyExerciseTime" to heavyExerciseTime
                    )
                )
            }
        }
        return ret
    }

    fun motionDailyData(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (item in motionDataBuffer) {
            ret.add(
                mapOf(
                    "seq" to ret.size,
                    "startDate" to item.time,
                    "endDate" to item.end,
                    "sportType" to 0,
                    "sportLevel" to item.dataType,
                    "heart" to item.rate,
                    "step" to item.steps,
                    "pressure" to item.preesure,
                    "oxgen" to item.oxgen,
                )
            )
        }
        return ret
    }

    private fun motionSportData(): List<Map<String, Any>> {
        var ret: MutableList<Map<String, Any>> = ArrayList()
        for (dict in dailyOrHistoryArray) {
            for (history in dict) {
                val start = Date(history.key)
                history.value.forEachIndexed { index, item ->
                    val s1 = addingTimeInterval(start, index.toLong())
                    ret.add(
                        mapOf(
                            "seq" to ret.size,
                            "startDate" to dateFmt.format(s1),
                            "endDate" to dateFmt.format(s1),
                            "sportType" to 1,
                            "sportLevel" to item.dataType,
                            "heart" to item.rate,
                        )
                    )
                }
            }
        }
        return ret
    }

    fun getMotionData(): List<Map<String, Any>> {
        return if (isSport) motionSportData() else motionDailyData()
    }

    fun maoV(type: Int): Int {
        when (type) {
            0 ->
                getRandomInt(200, 4000)
            1 ->
                60
            2 ->
                getRandomInt(50, 140)
            3 ->
                getRandomInt(0, 100)
            4 ->
                getRandomInt(20, 100)
            else -> {
                0
            }
        }
        return 0
    }

    private fun getRandomInt(from: Int, to: Int): Int {
        return (from..to).random()
    }

    private fun addingTimeInterval(date: Date, interval: Long): Date {
        val newDateTime = date.time + interval * 1000
        return Date(newDateTime)
    }

}
